<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * LoginForm is the model behind the login form.
 */
class LoginForm extends Model
{
    public $username;
    public $password;
    public $rememberMe = true;
    public $verifyCode;

    private $_user = false;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password', 'verifyCode'], 'required', 'on'=>'login'],
            [['username', 'password'], 'required', 'on'=>'intelnallogin'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
            //validate the verifyCode
            ['verifyCode','captcha','on'=>'login'],
        ];
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'username' => Yii::t('User', 'UserName'),
            'password' => Yii::t('User', 'Password'),
            'rememberMe' => Yii::t('User', 'RememberMe'),
            'verifyCode' => Yii::t('User', 'VerifyCode'),
       ];
    }



    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();
            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute,yii::t('User', 'Incorrect username or password.'));
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     * @return boolean whether the user is logged in successfully
     */
    public function login()
    {
        if ($this->validate()) {
            $user = $this->getUser();
            if(Yii::$app->user->login($user, $this->rememberMe ? 3600 * 24 * 7 : 0))
            {
                $ip = "unknown";
                if (getenv('HTTP_CLIENT_IP') &&
                    strcasecmp(getenv('HTTP_CLIENT_ID'), 'unknown')
                )
                {
                    $ip = getenv('HTTP_CLIENT_IP');
                }
                elseif(getenv('HTTP_X_FORWARDED_FOR') &&
                    strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')
                )
                {
                    $ip = getenv('HTTP_X_FORWARDED_FOR');
                }
                elseif (getenv('REMOTE_ADDR') &&
                    strcasecmp(getenv('REMOTE_ADDR'), 'unknown')
                )
                {
                    $ip = getenv('REMOTE_ADDR');
                }
                elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] &&
                    strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')
                )
                {
                    $ip = $_SERVER['REMOTE_ADDR'];
                }
                $user->last_login_ip = $ip;
                $user->last_login_date  = date('Y-m-d h:i:s', time());
                $user->update();
                return true;
            }
            else
            {
                return false;
            }
        } else {
            return false;
        }
    }
    /**
     * Finds user by [[username]]
     *
     * @return User|null
     */
    public function getUser()
    {
        if ($this->_user === false) {
            $this->_user = User::findByUsername($this->username);
        }
        return $this->_user;
    }
}
